<template>
    <div class="cssToolbar">
        <a-space>
            <Toolbar :items="['查询', '审核']" @select="cx" @audit="shenghe"></Toolbar>
        </a-space>
    </div>
    <!-- <a-table size="mini" :scrollbar="true" sticky-header @page-change="pagination.change" :style="{
        height: pageData.rHeight - 250 + 'px'
    }" stripe :bordered="true" :columns="tableColumns" :row-selection="{ showCheckedAll: true, width: 50 }"
        v-model:selected-keys="selectedRowKeys" :loading="table.tableLoading" :data="table.dataList" :pagination="false"
        :rowKey="rowKey">
        <template #planOrderState="{ record }">
            <a-dropdown @select="handleSelect">
                <a-button @click="handleSelects(record)" v-if="record.planOrderState == 2" type="primary" size="mini"
                    status="danger" shape="round">
                    紧急&nbsp
                    <icon-down />
                </a-button>
                <a-button @click="handleSelects(record)" v-if="record.planOrderState == 1" type="primary" size="mini"
                    status="success" shape="round">
                    &nbsp&nbsp急&nbsp&nbsp
                    <icon-down />
                </a-button>
                <a-button @click="handleSelects(record)" v-if="record.planOrderState == 0" status="danger" size="mini"
                    shape="round">
                    正常&nbsp
                    <icon-down />
                </a-button>
                <template #content>
                    <a-doption>紧急</a-doption>
                    <a-doption>急</a-doption>
                    <a-doption>正常</a-doption>
                </template>
            </a-dropdown>
        </template>
        <template #planState="{ record }">
            <span v-if="record.planState == 0">自行生产</span>
        </template>
        <template #slote="{ record }">
            <a-button type="primary" size="mini" status="warning" @click="editHandler(record)">
                <template #icon>
                    <icon-edit />
                </template>
                修 改
            </a-button>
        </template>
    </a-table> -->
      <AgGridVuesCopys :rowHeight="33" :headerHeight="33" :suppressCopyRowsToClipboard="true" rowSelection="multiple" @grid-ready="gridReady" style="height:90%" :defaultColDef="defaultColDef"
        class='ag-theme-alpine' :rowData="table.dataList" :columnDefs="tc" :localeText="localeText1"></AgGridVuesCopys>
    <paginationCopys :total="total" show-total show-jumper show-page-size @change="PageSize" @page-size-change="change1"
        style="justify-content: center" :page-size="size" :current="page" />
    <!-- sideNo1 -->
    <a-drawer :drawer-style="{ 'zIndex': 999 }" ok-text="保存" cancel-text="关闭" :width="800"
        popup-container=".main-base-style" :height="pageData.rHeight" :visible="pageData.showAdd" placement="bottom"
        unmountOnClose @ok="sideOk1" @cancel="sideNo1" :closable="false">
        <template #title>
            {{ isYupai }}
        </template>
        <div>
            <div>
                <a-form ref="formRef" :model="dataListss" size="mini" layout="vertical">
                    <div>
                        <a-space>
                            <a-form-item label="排产号" field="schedulCode">
                                <!-- 标记 -->
                                <a-input class="bacClass" disabled placeholder="" v-model="dataListss.schedulCode"
                                    :style="{ width: '250px' }" />
                            </a-form-item>
                            <!-- <a-form-item label="上机时间" field="computerTime"> -->
                            <!-- <a-select popup-container="html"  v-model="queryData.firm" :style="{ width: '240px' }"> -->
                            <!-- <a-option  value="">全部</a-option>
                                   <a-option v-for="(item,index) in pageData.clothList" :value="item.code" :key="index">{{item.code}}</a-option> -->
                            <!-- </a-select> -->
                            <!-- <a-date-picker  popup-container="html"  show-time :style="{ width: '250px' }"
                                    v-model="dataListss.computerTime" />
                            </a-form-item> -->
                            <a-form-item label="适用机型" field="applyMachine">
                                <!-- <a-select popup-container="html"  v-model="queryData.firm" :style="{ width: '240px' }"> -->
                                <!-- <a-option  value="">全部</a-option>
                                   <a-option v-for="(item,index) in pageData.clothList" :value="item.code" :key="index">{{item.code}}</a-option> -->
                                <!-- </a-select> -->
                                <a-input class="bacClass" v-model="dataListss.applyMachine" :style="{ width: '250px' }" />
                            </a-form-item>
                            <a-form-item label="指定楼层" field="storey">
                                <a-select popup-container="html"  class="bacClass" v-model="queryData.storey" :style="{ width: '240px' }" @change="storeyStor"
                                    allow-clear>
                                    <a-option v-for="item in pageData.clothListr" :value="item">{{
                                        item
                                    }}</a-option>
                                </a-select>

                            </a-form-item>
                            <a-form-item label="机台数">
                                <!-- <a-select popup-container="html"  class="bacClass" v-model="queryData.firm" :style="{ width: '240px' }"> -->
                                <!-- <a-option  value="">全部</a-option>
                                   <a-option v-for="(item,index) in pageData.clothList" :value="item.code" :key="index">{{item.code}}</a-option> -->
                                <!-- </a-select> -->
                                <a-input disabled v-model="macNum" :style="{ width: '250px' }" />
                            </a-form-item>
                        </a-space>
                    </div>
                </a-form>
                <a-table size="mini" stripe :bordered="true" :columns="tableColumns1" :data="dataListssz">
                </a-table>

                <div class="bg-content">
                    <a-checkbox-group @change="changeId" :default-value="changeIds">
                        <div v-for="(item, index) in dataListsss" :key="index">
                            <div :class="item.machineState == 0 ? 'lvClass' : 'huClass'">
                                <a-checkbox v-if="item.startDate == null" disabled @click="checkboxs"></a-checkbox>
                                <a-checkbox v-else v-model="item.machineId" :value="item.machineId"></a-checkbox>
                            </div>
                            <span>{{ item.machineCode }}</span>
                            <a-range-picker  popup-container="html"  class="bacClass" size="mini" v-model="item.startDate" showTime style=" width: 100px; " />
                        </div>
                    </a-checkbox-group>
                </div>
                <div :style="{ marginTop: '10px' }">
                    <a-button type="primary" @click="paichanxq">机台排产详情</a-button>
                </div>

            </div>
        </div>
    </a-drawer>
    <a-modal :width="1200" v-model:visible="visible" :on-before-ok="handleOk" @cancel="handleCancel">
        <template #title>
            机台排产详情
        </template>
        <div class="jitaipaic">
            <div><span>最大可排产天数{{ maxData }}</span></div>
            <div class="jitaipaic1"><span>计划重量{{ orderWeight }}KG</span></div>
        </div>
        <a-table :scrollbar="true" sticky-header size="mini" :columns="columns" :data="data" :pagination="false"
            :style="{ height: '500px' }">
            <!-- <template #act4="{ record }">
            <a-button type="primary" status="success" size="mini">修改</a-button>
        </template>
        <template #remark="{ record }">
            KG
        </template> -->
            <template v-for="(item, index) in columns" #[item.slotName]="{ record }">
                <!-- <a-button v-if="(index == columns.length - 1)" type="primary" status="success" size="mini">修改</a-button> -->
                <div>
                    <a-input-number class="bacClass" allow-clear v-model="record[item.slotName]">
                        <template #append>
                            KG
                        </template>
                    </a-input-number>
                </div>
            </template>
        </a-table>
    </a-modal>
</template>
 
<script lang='ts' setup>
import { reactive, toRefs, ref, onMounted } from 'vue'
import { useTable, useRowKey, useTableColumn, usePagination, useRowSelection } from '@/hooks/table'
import Toolbar from '@/components/Toolbar.vue'
import { post } from '@/api/http'
import { Message } from '@arco-design/web-vue'
import { Modal } from '@arco-design/web-vue'
import { ValueData } from '@arco-design/web-vue/es/_utils/types'

import {localeText} from '@/api/aggridZw'
const localeText1:any=ref(localeText)
const gridOption = ref()
const gridReady = (params: any) => {
    gridOption.value = params.api
}
const { selectedRowKeys } = useRowSelection()
onMounted(() => {
    pageData.rHeight = document.getElementsByClassName("main-section")[0]?.clientHeight
    //弹出层的高
    if (pageData.rHeight !== undefined) {
        pageData.rHeight = pageData.rHeight;
    }
    // doRefresh()
})

// let rangeValue = ref()
const total = ref(10);
const page = ref(1);
const size = ref(10);
function PageSize(val: any) {
    page.value = val
    doRefresh();
}
function change1(val: any) {
    size.value = val
    doRefresh();
}

function doRefresh() {
    post({
        url: "planSingle/planSchedul/getPageInfo",
        data: {
            page: page.value,
            size: size.value,
            deleteState: 0,
            schedulState: 1
        },
    })
        .then(({ data, code }: any) => {
            if (code === 200) {
                lc();
                gridOption.value.setRowData(data.list)
                table.handleSuccess(data.list);
                total.value = data.total;
            }
        })
        .catch(console.log);
}

const checkboxs = () => {
    Message.error('请先选择时间')
}

const cx = () => {
    pagination.page = 1
    doRefresh()
}
const handleSelect = (key: any) => {
    let ids = 0
    switch (key) {
        case '紧急':
            ids = 2
            break;
        case '急':
            ids = 1
            break;
        case '正常':
            ids = 0
            break;
        default:
            break;
    }
    post({
        url: "planSingle/planSchedul/update",
        data: [{
            id: Number(selectIsh.id),
            planOrderState: Number(ids)
        }],
    }).then((res: any) => {
        if (res.code === 200) {
            doRefresh()
            console.log('666' + table.dataList)
            Message.success('修改成功')
        } else {
            Message.error(res.message)
        }
    })
}
let selectIsh: {
    id: any
} = {
    id: ''
}
//btn获取当前行信息
const handleSelects = (key: any) => {
    selectIsh = key
}
const pagination = usePagination(doRefresh)
const rowKey = useRowKey("id")
const queryData = reactive({
    firm: "",
    clothType: "",
    storey: ''
})
const delWeipaichan = () => {
    let isArray: any[] = []
    selectedRowKeys.value.forEach((item) => {
        isArray.push(item)
    })
    if (selectedRowKeys.value.length === 0) {
        Message.error('请先勾选')
        return
    }
    Modal.confirm({
        title: '系统提示',
        content: '确认删除选中的数据?',
        onOk() {
            // console.log(selectedRowKeys.value.values)
            post({
                url: 'planSingle/planSchedul/delete',
                data: isArray
            }).then(({ code, data }: any) => {
                if (code === 200) {
                    doRefresh()
                    selectedRowKeys.value = []
                    Message.success({
                        content: '删除成功',
                        duration: 5000
                    })
                } else {
                    Message.success({
                        content: '删除失败',
                        duration: 5000
                    })
                }
            })
        }
    })
}
const paic = () => {
    if (selectedRowKeys.value.length === 0) {
        Message.error('请先勾选')
        return
    }
    if (selectedRowKeys.value.length > 1) {
        Message.error('只能勾选一条')
    }
    // Modal.success({
    //     title: '系统提示',
    //     content: '订单审核通过已到已排产界面',
    //     onOk() {
    //         post({
    //             url: "planSingle/planSchedul/update",
    //             data: [{
    //                 id: Number(selectedRowKeys.value),
    //                 schedulState: Number(2)
    //             }],
    //         }).then((res: any) => {
    //             if (res.code === 200) {
    //                 doRefresh()
    //                 Message.success('修改成功')
    //             } else {
    //                 Message.error(res.message)
    //             }
    //         })
    //     }
    // })
    let firm = ''
    let planOrderId = ''
    let planOrderCode = ''
    table.dataList.forEach(item => {
        if (item.id == selectedRowKeys.value[0]) {
            firm = item.firm
            planOrderId = item.planOrderId
            planOrderCode = item.planOrderCode
            dataListss.schedulCode = item.schedulCode
            dataListss.computerTime = item.computerTime
            dataListss.applyMachine = item.applyMachine
            dataListss.firm = item.firm
            queryData.storey = item.storey
            dataListss.defaultNum = item.defaultNum
            dataListss.planOrderId = item.planOrderId
            dataListss.planOrderCode = item.planOrderCode
        }
    })
    // isDeFou(firm, planOrderId, Number(selectedRowKeys.value), planOrderCode, dataListss.applyMachine, dataListss.storey)
    post({
        url: "planSingle/planSchedulDetail/getList",
        data: {
            id: Number(selectedRowKeys.value),
            firm,
            planOrderId,
            planOrderCode,
            applyMachine: dataListss.applyMachine,
            storey: queryData.storey,
        },
    }).then((res: any) => {
        if (res.code == 200) {
            macNum.value = res.data.machineNum
            dataListssz.value = [res.data.planSchedulDetails[0]]
            dataListsss.value = res.data.planSchedulLooms
            post({
                url: 'planSingle/planSchedul/getAutomaticInfo'
            }).then((res: any) => {
                if (res.code == 200) {
                    dataListss.schedulCode = res.data.schedulCode
                    pageData.showAdd = true
                }
            })
            post({
                url: "planSingle/planSchedulDetail/getMachine",
                data: {
                    id: Number(selectedRowKeys.value),
                    firm,
                    planOrderId,
                    planOrderCode,
                    applyMachine: dataListss.applyMachine,
                    storey: queryData.storey,
                },
            }).then((res: any) => {
                if (res.code == 200) {
                    res.data.forEach((item: any) => {
                        item.startDate = [item.startDate, item.endDate]
                    })
                    dataListsss.value.unshift(...res.data)
                    if (res.data.length != 0) {
                        res.data.forEach((item: any) => {
                            changeIds.value.unshift(item.machineId)
                        })
                    }
                }
            })
            // if (res.data.schedulPlanSchedulLooms.length != 0) {
            pageData.showAdd = true
            isYupai.value = '排产'
            // } else {
            //     Message.error('还未预排')
            // }
        }
    })
}

const exports = () => {
    if (selectedRowKeys.value.length === 0) {
        Message.error('请先勾选')
        return
    }
    Modal.confirm({
        title: '系统提示',
        content: '是否导出选择列表',
        onOk() {
            let table = document.getElementsByTagName("table")[0]
            tableToXlsx(table, '计划单未排产')
            console.log(table)
        }
    });
}

function tableToXlsx(table: any, filename: any) {
    const uri = 'data:application/vnd.ms-excel;base64,',
        template = `<html>
            <head><meta charset="UTF-8"></head>
            <body><table border="1">${table.innerHTML}</table></body>
        </html>`;

    const a = document.createElement('a');
    a.href = uri + window.btoa(unescape(encodeURIComponent(template)));
    a.download = filename;
    a.click();
    document.removeChild(a);
}
const tableColumns = useTableColumn([
    {
        title: "排产号",
        key: "schedulCode",
        dataIndex: "schedulCode",
        width: 150,
    },
    {
        title: "计划单号",
        key: "planOrderCode",
        dataIndex: "planOrderCode",
        width: 150,
    },
    // {
    //     title: "计划类型",
    //     key: "planType",
    //     dataIndex: "planType",
    //     width: 150,
    // },
    {
        title: "客户",
        key: "customName",
        dataIndex: "customName",
        width: 150,
    },
    {
        title: "机台号",
        key: "schedulMachineCode",
        dataIndex: "schedulMachineCode",
        width: 150,
    },
    {
        title: "坯布编号",
        key: "greyCode",
        dataIndex: "greyCode",
        width: 200,
    },
    {
        title: "坯布名称",
        key: "greyName",
        dataIndex: "greyName",
        width: 450,
    },
    {
        title: "产品类别",
        key: "productType",
        dataIndex: "productType",
        width: 150,
    },
    {
        title: "面料种类",
        key: "fabricType",
        dataIndex: "fabricType",
        width: 150,
    },
    {
        title: "品牌",
        key: "brandName",
        dataIndex: "brandName",
        width: 150,
        align: 'left'
    },
    {
        title: "下单重量",
        key: "orderWeight",
        dataIndex: "orderWeight",
        width: 150,
    },
    // {
    //     title: "加工单价",
    //     key: "machinPrice",
    //     dataIndex: "machinPrice",
    //     width: 150,
    // },
    // {
    //     title: "加工金额",
    //     key: "machinMoney",
    //     dataIndex: "machinMoney",
    //     width: 150,
    // },
    {
        title: "计划单状态",
        key: "planOrderState",
        dataIndex: "planOrderState",
        slotName: 'planOrderState',
        width: 150,
        fixed: 'right'
    },
    // {
    //     title: "订单交期",
    //     key: "orderDeliveryDate",
    //     dataIndex: "orderDeliveryDate",
    //     width: 150,
    // },
    {
        title: "计划状态",
        key: "planState",
        dataIndex: "planState",
        slotName: 'planState',
        width: 150,
    },
    {
        title: "操作",
        key: "slote",
        dataIndex: "slote",
        slotName: 'slote',
        width: 150,
        fixed: 'right'
    },
])

const tableColumns1 = useTableColumn([
    {
        title: "织造序号",
        key: "weaveCode",
        dataIndex: "weaveCode",
        width: 150,
    },
    {
        title: "坯布编号",
        key: "greyCode",
        dataIndex: "greyCode",
        width: 150,
    },
    {
        title: "坯布名称",
        key: "greyName",
        dataIndex: "greyName",
        width: 450,
    },
    {
        title: "产品类别",
        key: "productType",
        dataIndex: "productType",
        width: 150,
    },
    {
        title: "计划总量",
        key: "planCount",
        dataIndex: "planCount",
        width: 150,
    },
    {
        title: "已排量",
        key: "alreadySchedulingNum",
        dataIndex: "alreadySchedulingNum",
        width: 150,
    },
    {
        title: "本次排程量",
        key: "onceSchedulingNum",
        dataIndex: "onceSchedulingNum",
        width: 150,
    },
    {
        title: "适用机型",
        key: "applyMachine",
        dataIndex: "applyMachine",
        width: 150,
    },
    {
        title: "最晚交期",
        key: "lastDeliveryTime",
        dataIndex: "lastDeliveryTime",
        width: 150,
    },
])

interface isDatalist {
    id: any
    schedulCode: any
    computerTime: any
    applyMachine: any
    firm: any
    storey: any
    defaultNum: any
    planOrderId: any,
    planOrderCode: any
}

let dataListss = reactive<isDatalist>(
    {
        id: '',
        schedulCode: '',
        computerTime: '',
        applyMachine: '',
        firm: '',
        storey: '',
        defaultNum: '',
        planOrderId: null,
        planOrderCode: null
    }
)

let dataListsss = ref<any[]>([])
const table = useTable()
const pageData = reactive({
    showAdd: false,
    rHeight: 0,
    clothListr: []
})
function lc() {
    post({
        url: "/planSingle/planSchedul/getMachineAreaPara",
        data: {
            firm: ""
        },
    }).then((res: any) => {
        if (res.code == 200) {

            pageData.clothListr = res.data
        }
    })
}
const storeyStor = (val: any) => {

    const resDtas = table.dataList.filter(item => {
        return item.id == selectedRowKeys.value[0]
    })
    console.log(selectedRowKeys.value)
    post({
        url: "planSingle/planSchedulDetail/getList",
        data: {
            id: resDtas[0].id,
            firm: resDtas[0].firm,
            planOrderId: resDtas[0].planOrderId,
            planOrderCode: resDtas[0].planOrderCode,
            applyMachine: resDtas[0].applyMachine,
            storey: queryData.storey
        },
    }).then((res: any) => {
        if (res.code == 200) {
            macNum.value = res.data.machineNum
            dataListssz.value = [res.data.planSchedulDetails[0]]
            dataListsss.value = res.data.planSchedulLooms
            post({
                url: "planSingle/planSchedulDetail/getMachine",
                data: {
                    id: resDtas[0].id,
                    firm: resDtas[0].firm,
                    planOrderId: resDtas[0].planOrderId,
                    planOrderCode: resDtas[0].planOrderCode,
                    applyMachine: resDtas[0].applyMachine,
                    storey: queryData.storey
                },
            }).then((res: any) => {
                if (res.code == 200) {
                    res.data.forEach((item: any) => {
                        item.startDate = [item.startDate, item.endDate]
                    })
                    dataListsss.value.unshift(...res.data)
                    if (res.data.length != 0) {
                        res.data.forEach((item: any) => {
                            changeIds.value.unshift(item.machineId)
                        })
                    }
                }
            })
        }
    })
}
let yupaicId: any;
let isRedis: any;
const editHandler = async (record: any) => {
    const res: any = await post({
        url: 'planSingle/planSchedul/redisOut',
        data: [record.planOrderCode]
    })
    if (res.code == 200) {
        selectedRowKeys.value = [record.id]
        isYupai.value = '修改预排产'
        dataListss.schedulCode = record.schedulCode
        dataListss.computerTime = record.computerTime
        dataListss.applyMachine = record.applyMachine
        dataListss.firm = record.firm
        queryData.storey = record.storey
        dataListss.defaultNum = record.defaultNum
        dataListss.planOrderId = record.planOrderId
        dataListss.planOrderCode = record.planOrderCode
        // isDeFou(record.firm, record.planOrderId, record.id, record.planOrderCode, record.applyMachine, record.storey)
        post({
            url: "planSingle/planSchedulDetail/getList",
            data: {
                // id: Number(record.id),
                id: Number(gridOption.value.getSelectedRows().map((o: any) => { return o.id })),
                firm: record.firm,
                planOrderId: record.planOrderId,
                planOrderCode: record.planOrderCode,
                applyMachine: record.applyMachine,
                storey: record.storey,
            },
        }).then((res: any) => {
            if (res.code == 200) {
                macNum.value = res.data.machineNum
                dataListssz.value = [res.data.planSchedulDetails[0]]
                // dataListsss.value = res.data.schedulPlanSchedulLooms
                dataListsss.value = res.data.planSchedulLooms
                // console.log(res.data.schedulPlanSchedulLooms)
                // if (res.data.schedulPlanSchedulLooms.length != 0) {
                //     res.data.schedulPlanSchedulLooms.forEach((item: any) => {
                //         changeIds.value.unshift(item.id)
                //         console.log(item)
                //     })
                // }
                // console.log(changeIds.value)
                post({
                    url: "planSingle/planSchedulDetail/getMachine",
                    data: {
                        id: Number(gridOption.value.getSelectedRows().map((o: any) => { return o.id })),
                        firm: record.firm,
                        planOrderId: record.planOrderId,
                        planOrderCode: record.planOrderCode,
                        applyMachine: record.applyMachine,
                        storey: record.storey,
                    },
                }).then((res: any) => {
                    if (res.code == 200) {
                        res.data.forEach((item: any) => {
                            item.startDate = [item.startDate, item.endDate]
                        })
                        dataListsss.value.unshift(...res.data)
                        if (res.data.length != 0) {
                            res.data.forEach((item: any) => {
                                changeIds.value.unshift(item.machineId)
                            })
                        }
                        pageData.showAdd = true
                    }
                })
            }
        })

        yupaicId = gridOption.value.getSelectedRows().map((o: any) => { return o.id })
    } else {
        Message.error(res.message)
    }

    isRedis = setInterval(() => {
        post({
            url: 'planSingle/planSchedul/redisOut',
            data: [record.planOrderCode]
        })
    },290000)

}

let isYupai = ref()

const yupaichan = () => {

    for (let item in dataListss) {
        dataListss[item] = ''
    }
    isYupai.value = '预排产'
    if (selectedRowKeys.value.length === 0) {
        Message.error('请先勾选')
        return
    }
    if (selectedRowKeys.value.length > 1) {
        Message.error('只能勾选一条')
        return
    }
    let firm = ''
    let planOrderId = ''
    let planOrderCode = ''
    let applyMachine = ''
    let storey = ''
    table.dataList.forEach(item => {
        if (item.id == selectedRowKeys.value[0]) {
            firm = item.firm
            planOrderId = item.planOrderId
            planOrderCode = item.planOrderCode
            applyMachine = item.applyMachine
            storey = item.storey
            queryData.storey = item.storey
            dataListss.schedulCode = item.schedulCode
            dataListss.computerTime = item.computerTime
            dataListss.applyMachine = item.applyMachine
            dataListss.firm = item.firm
            dataListss.storey = item.storey
            dataListss.defaultNum = item.defaultNum
            dataListss.planOrderId = item.planOrderId
            dataListss.planOrderCode = item.planOrderCode
        }
    })
    // isDeFou(firm, planOrderId, Number(selectedRowKeys.value), planOrderCode, applyMachine, storey)
    post({
        url: "planSingle/planSchedulDetail/getList",
        data: {
            id: Number(selectedRowKeys.value),
            firm,
            planOrderId,
            planOrderCode,
            applyMachine,
            storey
        },
    }).then((res: any) => {
        if (res.code == 200) {
            macNum.value = res.data.machineNum
            dataListssz.value = [res.data.planSchedulDetails[0]]
            dataListsss.value = res.data.planSchedulLooms
            post({
                url: 'planSingle/planSchedul/getAutomaticInfo'
            }).then((res: any) => {
                if (res.code == 200) {
                    dataListss.schedulCode = res.data.schedulCode
                    pageData.showAdd = true
                }
            })
        }
    })
}
let dataListssz: any = ref([])
// function isDeFou(firm: any, planOrderId: any, id: any, planOrderCode: any, applyMachine: any, storey: any) {
//     post({
//         url: "planSingle/planSchedulDetail/getList",
//         data: {
//             id,
//             firm,
//             planOrderId,
//             planOrderCode,
//             applyMachine,
//             storey,
//         },
//     }).then((res: any) => {
//         if (res.code === 200) {
//             dataListssz.value = [res.data.planSchedulDetails[0]]
//             dataListsss.value = res.data.planSchedulLooms
//             if (res.data.schedulPlanSchedulLooms.length != 0) {
//                 res.data.schedulPlanSchedulLooms.forEach((item: any) => {
//                     dataListsss.value.unshift({
//                         id: item.id,
//                         firm: item.firm,
//                         machineCode: item.machineCode,
//                         startDate: [item.startDate, item.endDate]
//                     })
//                     changeIds.value.unshift(item.id)
//                 })
//             }
//         } else {
//             Message.error(res.message)
//         }
//     })
// }

let changeIdArry: any[] = []
let changeIds = ref<any[]>([])
const changeId = (value: any) => {
    changeIds.value = value
    console.log(changeIds.value)
    console.log(selectedRowKeys.value)
    const taData = table.dataList.filter((item: any) => {
        return selectedRowKeys.value[0] == item.id
    })
    let isDataList: any = []
    value.forEach((is: any) => {
        dataListsss.value.forEach((item: any) => {
            if (is == item.machineId) {
                isDataList.push({
                    createName: item.createName,
                    createTime: item.createTime,
                    deleteState: item.deleteState,
                    endDate: item.startDate[1],
                    firm: item.firm,
                    id: item.id,
                    machineCode: item.machineCode,
                    machineId: item.machineId,
                    machineName: item.machineName,
                    obsoleteState: item.obsoleteState,
                    planOrderCode: item.planOrderCode,
                    planSchedulId: item.planSchedulId,
                    remark: item.remark,
                    startDate: item.startDate[0],
                    storey: item.storey
                })
            }
        })
    })
    post(
        {
            url: 'planSingle/planSchedulDetail/schedulMachine',
            data: {
                ...taData[0],
                machine: isDataList
            }
        }
    ).then((res: any) => {
        if (res.code == 200) {
            post({
                url: "planSingle/planSchedulDetail/getList",
                data: {
                    id: Number(selectedRowKeys.value),
                    firm: taData[0].firm,
                    planOrderId: taData[0].planOrderId,
                    planOrderCode: taData[0].planOrderCode,
                    applyMachine: taData[0].applyMachine,
                    storey: queryData.storey
                },
            }).then((res: any) => {
                if (res.code == 200) {
                    macNum.value = res.data.machineNum
                    dataListssz.value = [res.data.planSchedulDetails[0]]
                    dataListsss.value = res.data.planSchedulLooms
                    post({
                        url: "planSingle/planSchedulDetail/getMachine",
                        data: {
                            id: Number(selectedRowKeys.value),
                            firm: taData[0].firm,
                            planOrderId: taData[0].planOrderId,
                            planOrderCode: taData[0].planOrderCode,
                            applyMachine: taData[0].applyMachine,
                            storey: queryData.storey
                        },
                    }).then((res: any) => {
                        if (res.code == 200) {
                            res.data.forEach((item: any) => {
                                item.startDate = [item.startDate, item.endDate]
                            })
                            dataListsss.value.unshift(...res.data)
                            // if (res.data.length != 0) {
                            //     res.data.forEach((item: any) => {
                            //         changeIds.value.unshift(item.machineId)
                            //     })
                            // }
                        }
                    })
                }
            })
        } else {
            changeIds.value = []
            Message.error(res.message)
        }
    })

}
const sideOk1 = () => {
    if (isYupai.value == '预排产') {
        let planOrderCodes: any;
        table.dataList.forEach((item: any) => {
            if (item.id == selectedRowKeys.value[0]) {
                planOrderCodes = item.planOrderCode
            }
        })
        let ischangeIdArrys: any = [];
        for (let i = 0; i < changeIds.value.length; i++) {
            dataListsss.value.forEach((item: any) => {
                if (item.machineId == changeIds.value[i]) {
                    ischangeIdArrys.push({
                        id: item.id,
                        firm: item.firm,
                        planOrderCode: planOrderCodes,
                        machineCode: item.machineCode,
                        startDate: item.startDate[0],
                        endDate: item.startDate[1],
                        storey: item.storey
                    })
                }
            })
        }
        if (ischangeIdArrys.length == 0) {
            Message.error('请选择机台')
            return
        }
        let planOrderCode;
        let customName;
        table.dataList.forEach((item: any) => {
            if (item.id == selectedRowKeys.value[0]) {
                planOrderCode = item.planOrderCode
                customName = item.customName
            }
        })
        let weaveCode;
        let greyCode;
        let greyName;
        dataListssz.value.forEach((item: any) => {
            weaveCode = item.weaveCode
            greyCode = item.greyCode
            greyName = item.greyName
        })
        post({
            url: 'planSingle/planSchedulDetail/temporarySave',
            data: {
                id: Number(selectedRowKeys.value),
                planOrderCode: planOrderCode,
                customName: customName,
                schedulCode: dataListss.schedulCode,
                computerTime: dataListss.computerTime,
                applyMachine: dataListss.applyMachine,
                storey: queryData.storey,
                defaultNum: dataListss.defaultNum,
                planSchedulLooms: ischangeIdArrys,
                weaveCode,
                greyCode,
                greyName,
                schedulState: 1
            }
        }).then((res: any) => {
            if (res.code == 200) {
                doRefresh()
                clearInterval(isRedis)
                post({
                    url: 'planSingle/planSchedul/upRedis'
                })
                pageData.showAdd = false
                selectedRowKeys.value = []
                changeIds.value = []
                dataListsss.value = []
                Message.success('保存成功')
            } else {
                Message.success(res.message)
            }
        })
    } else if (isYupai.value == '修改预排产') {
        let planOrderCodes: any;
        let customNames;
        table.dataList.forEach((item: any) => {
            if (item.id == yupaicId) {
                planOrderCodes = item.planOrderCode
                customNames = item.customName
            }
        })
        let ischangeIdArrys: any = [];
        for (let i = 0; i < changeIds.value.length; i++) {
            dataListsss.value.forEach((item: any) => {
                if (item.machineId == changeIds.value[i]) {
                    ischangeIdArrys.push({
                        id: item.id,
                        firm: item.firm,
                        planOrderCode: planOrderCodes,
                        machineCode: item.machineCode,
                        startDate: item.startDate[0],
                        endDate: item.startDate[1],
                        storey: item.storey
                    })
                }
            })
        }
        let weaveCode;
        let greyCode;
        let greyName;
        dataListssz.value.forEach((item: any) => {
            weaveCode = item.weaveCode
            greyCode = item.greyCode
            greyName = item.greyName
        })
        post({
            url: 'planSingle/planSchedulDetail/update',
            data: {
                id: Number(yupaicId),
                planOrderCode: planOrderCodes,
                customName: customNames,
                schedulCode: dataListss.schedulCode,
                computerTime: dataListss.computerTime,
                applyMachine: dataListss.applyMachine,
                storey: queryData.storey,
                defaultNum: dataListss.defaultNum,
                planSchedulLooms: ischangeIdArrys,
                weaveCode,
                greyCode,
                greyName,
            }
        }).then((res: any) => {
            if (res.code == 200) {
                doRefresh()
                clearInterval(isRedis)
                post({
                    url: 'planSingle/planSchedul/upRedis'
                })
                pageData.showAdd = false
                Message.success('保存成功')
                selectedRowKeys.value = []
                changeIds.value = []
                dataListsss.value = []
            } else {
                Message.error(res.message)
            }
        })
    } else if (isYupai.value == '排产') {
        let planOrderId: any
        let planOrderCodes: any
        let customNames: any
        let drawnNeedleState: any
        table.dataList.forEach((item: any) => {
            if (item.id == selectedRowKeys.value[0]) {
                planOrderId = item.planOrderId
                customNames = item.customName,
                    drawnNeedleState = item.drawnNeedleState
                planOrderCodes = item.planOrderCode
            }
        })
        let weaveCode: any;
        let greyCode: any;
        let greyName: any;
        dataListssz.value.forEach((item: any) => {
            weaveCode = item.weaveCode
            greyCode = item.greyCode
            greyName = item.greyName
        })
        let ischangeIdArrys: any = [];
        for (let i = 0; i < changeIds.value.length; i++) {
            dataListsss.value.forEach((item: any) => {
                if (item.machineId == changeIds.value[i]) {
                    ischangeIdArrys.push({
                        id: item.id,
                        planOrderCode: planOrderCodes,
                        machineCode: item.machineCode,
                        startDate: item.startDate[0],
                        endDate: item.startDate[1],
                        weaveCode,
                        greyCode,
                        greyName,
                        customName: customNames,
                        drawnNeedleState: drawnNeedleState,
                        storey: item.storey
                    })
                }
            })
        }
        post({
            url: 'planSingle/planSchedulDetail/save',
            data: {
                id: selectedRowKeys.value[0],
                planOrderId,
                planOrderCode: planOrderCodes,
                planSchedulLooms: ischangeIdArrys,
                schedulCode: dataListss.schedulCode,
                weaveCode,
                greyCode,
                greyName,
                storey: queryData.storey,
                schedulState: 2
            }
        }).then((res: any) => {
            if (res.code == 200) {
                pageData.showAdd = false
                doRefresh()
                clearInterval(isRedis)
                post({
                    url: 'planSingle/planSchedul/upRedis'
                })
                changeIds.value = []
                Message.success('订单排产')
                selectedRowKeys.value = []
            }
        })
    }
}

const sideNo1 = () => {
    if (changeIds.value.length != 0 && isYupai.value == '预排产') {
        Message.error('请先取消勾选的机器')
        return
    }
    clearInterval(isRedis)
    post({
        url: 'planSingle/planSchedul/upRedis'
    })
    changeIds.value = []
    changeIdArry = []
    selectedRowKeys.value = []
    pageData.showAdd = false
}
const visible = ref(false)
const data: any = ref([])
const paichanxq = () => {
    if (changeIds.value.length == 0) {
        Message.error('请先选择机台')
        return
    }
    let jitaiArray: any = []
    let planOrderCode;
    changeIds.value.forEach((item: any) => {
        dataListsss.value.forEach((items: any) => {
            if (item == items.machineId) {
                planOrderCode = items.planOrderCode
                jitaiArray.push(
                    {
                        machineCode: items.machineCode,
                        startDate: items.startDate[0],
                        endDate: items.startDate[1]
                    }
                )
            }
        })
    })
    post({
        url: 'planSingle/machineSchedul/getData',
        data: {
            planOrderCode,
            machines: jitaiArray
        }
    }).then((res: any) => {
        if (res.code == 200) {
            for (let i = 0; i < res.data.days.length; i++) {
                // columns.value.splice(columns.value.length - 1, 0, {
                //     title: res.data.days[i],
                //     key: res.data.days[i],
                //     dataIndex: res.data.days[i],
                //     slotName: res.data.days[i],
                //     width: 150
                // })
                columns.value.push({
                    title: res.data.days[i],
                    key: res.data.days[i],
                    dataIndex: res.data.days[i],
                    slotName: res.data.days[i],
                    width: 200
                })
            }
            data.value = res.data.machineSchedul
            maxData.value = res.data.maxData
            orderWeight.value = res.data.orderWeight
            visible.value = true
        } else {
            Message.error(res.message)
        }
    })
    // visible.value = true
}
const maxData: any = ref()
const orderWeight: any = ref()
const handleOk = () => {
    // selectedRowKeys
    let jihudanhao: any;
    let pibubianhao: any;
    let pibuName: any;
    table.dataList.forEach((item: any) => {
        if (selectedRowKeys.value[0] == item.id) {
            jihudanhao = item.planOrderCode
            pibubianhao = item.greyCode
            pibuName = item.greyName
        }
    })
    let datas: any = []
    data.value.forEach((item: any) => {
        datas.push({
            ...item,
            planOrderCode: jihudanhao,
            greyCode: pibubianhao,
            greyName: pibuName
        })
    })
    post({
        url: 'planSingle/machineSchedul/insert',
        data: datas
    }).then((res: any) => {
        if (res.code == 200) {
            Message.success(res.message)
            visible.value = false
            columns.value = [
                {
                    title: "机台号",
                    key: "machineCode",
                    dataIndex: "machineCode",
                    width: 150
                },
                {
                    title: "理论产量",
                    key: "machineTargetCapacity",
                    dataIndex: "machineTargetCapacity",
                    width: 150
                },
                {
                    title: "极限日产能",
                    key: "maxMachineTargetCapacity",
                    dataIndex: "maxMachineTargetCapacity",
                    width: 150
                },
                // {
                //     title: "操作",
                //     key: "id",
                //     dataIndex: "id",
                //     slotName: "act4",
                //     width: 100,
                //     fixed: 'right'
                // },
            ]
            return true
        } else {
            Message.error(res.message)
            return false
        }
    })
}
const handleCancel = () => {
    columns.value = [
        {
            title: "机台号",
            key: "machineCode",
            dataIndex: "machineCode",
            width: 150
        },
        {
            title: "理论产量",
            key: "machineTargetCapacity",
            dataIndex: "machineTargetCapacity",
            width: 150
        },
        {
            title: "极限日产能",
            key: "maxMachineTargetCapacity",
            dataIndex: "maxMachineTargetCapacity",
            width: 150
        },
        // {
        //     title: "操作",
        //     key: "id",
        //     dataIndex: "id",
        //     slotName: "act4",
        //     width: 100,
        //     fixed: 'right'
        // },
    ]
    visible.value = false
}
const columns: any = ref([
    {
        title: "机台号",
        key: "machineCode",
        dataIndex: "machineCode",
        width: 150
    },
    {
        title: "理论产量",
        key: "machineTargetCapacity",
        dataIndex: "machineTargetCapacity",
        width: 150
    },
    {
        title: "极限日产能",
        key: "maxMachineTargetCapacity",
        dataIndex: "maxMachineTargetCapacity",
        width: 150
    },
    // {
    //     title: "操作",
    //     key: "id",
    //     dataIndex: "id",
    //     slotName: "act4",
    //     width: 100,
    //     fixed: 'right'
    // },
])
const shenghe = () => {
    if (gridOption.value.getSelectedRows().length === 0) {
        Message.error('请先勾选')
        return
    }
    if (gridOption.value.getSelectedRows().length > 1) {
        Message.error('只能勾选一条')
    }
    const isArray = table.dataList.filter((val: any) => {
        return val.id == Number(gridOption.value.getSelectedRows().map((o: any) => { return o.id }))
    })
    console.log(isArray)
    Modal.confirm({
        title: '系统提示',
        content: '订单审核通过已到已排产界面',
        onOk() {
            post({
                url: "planSingle/planSchedul/update",
                data: [{
                    id: Number(gridOption.value.getSelectedRows().map((o: any) => { return o.id })),
                    planOrderCode: isArray[0].planOrderCode,
                    schedulState: Number(2)
                }],
            }).then((res: any) => {
                if (res.code === 200) {
                    post({
                        url: 'planSingle/planSchedul/create',
                        data: isArray
                    }).then((res: any) => {
                        if (res.code != 200) {
                            Message.error(res.message)
                        }
                    })
                    doRefresh()
                    selectedRowKeys.value = []
                    Message.success('审核成功')
                } else {
                    Message.error(res.message)
                }
            })
        }
    })
}
let tc: any[] = tableColumns.map((o: any) => {

    let obj = {
        headerName: o.title,
        field: o.key,
        pinned: o['fixed']
    }
    if (o.slotName === 'planOrderState') {
        obj['resizable'] = false
        obj['filter'] = false
        obj['sortable'] = false
        obj["lockPosition"] = "right"
        obj['width'] = 150
        obj['cellRenderer'] = {
            template: `<a-dropdown   @select="c($event)">
                <a-button @click="a" v-if="b== 2" type="primary" size="mini"
                    status="danger" shape="round">
                    紧急&nbsp
                    <icon-down />
                </a-button>
                <a-button @click="a" v-if="b == 1" type="primary" size="mini"
                    status="success" shape="round">
                    &nbsp&nbsp急&nbsp&nbsp
                    <icon-down />
                </a-button>
                <a-button @click="a" v-if="b == 0" status="danger" size="mini"
                    shape="round">
                    正常&nbsp
                    <icon-down />
                </a-button>
                <template #content>
                    <a-doption>紧急</a-doption>
                    <a-doption>急</a-doption>
                    <a-doption>正常</a-doption>
                </template >
               
            </a-dropdown>`
            ,
            setup(props: any) {
                const a = () => {
                    handleSelects(props.params.data)
                }
                const c = ($event: any) => {
                    handleSelect($event)
                }
                return {
                    b: props.params.data.planOrderState,
                    a,
                    c,
                    d: props.params.data,
                    e: props.params.data.planState
                }
            }

        }
    }
    if (o.key == "planState") {
        obj['resizable'] = false
        obj['filter'] = false
        obj['sortable'] = false
        obj['cellRenderer'] = {
            template: ` <span v-if="b == 0">自行生产</span>`,
            setup(props: any) {
                return {
                    b: props.params.data.planState,

                }
            }
        }


    }
    if (o.key == "slote") {
        obj['resizable'] = false
        obj['filter'] = false
        obj['sortable'] = false
        obj["lockPosition"] = "right"
        obj['width'] = 100
        obj['cellRenderer'] = {
            template: `   <a-button type="primary" size="mini" status="warning" @click="b"> 修 改</a-button>`,
            setup(props: any) {
                const b = () => {
                    editHandler(props.params.data)
                }
                return {
                    b

                }
            }
        }
    }

    return obj
})

let a = [{
    showRowGroup: 'hide_def',
    "headerName": "",
    "checkboxSelection": true,
    "headerCheckboxSelection": true,
    "pinned": "left",
    "width": 50,
    resizable: false,
    filter: false,
    sortable: false,
    minWidth: 50,
    maxWidth: 50,
    lockPosition: "left",
}]
tc = a.concat(tc)
const defaultColDef = {
    sortable: true,
    resizable: true,
    filter: true,
    // flex: 1
}
components: {  }
const macNum:any = ref('')
</script>
<style lang='less' scoped>
.cssToolbar {
    margin-bottom: 10px;
    text-align: right;
}

.bg-content {
    width: 100%;
    margin-top: 10px;

    span {
        width: 100% !important;
        border-top: 1px dashed #ccc !important;
        justify-content: space-around !important;
        display: flex !important;
        flex-wrap: wrap !important;
        text-align: center !important;
    }

    div {
        margin-top: 10px;
        text-align: left;

        div {
            width: 100px;
            height: 100px;
            border: 1px solid #ccc;
        }
    }
}

.huClass {
    background-color: #FFFDBB;
}

.lvClass {
    background-color: #A4FFCB;
}

// ::v-deep .arco-drawer {
//     position: absolute;
//     display: flex;
//     flex-direction: column;
//     width: 100%;
//     height: 100%;
//     overflow: auto;
//     line-height: 1.5715;
//     background-color: var(--color-bg-3);
//     z-index: 9999;
// }
.jitaipaic {
    display: flex;

    .jitaipaic1 {
        margin-left: 50px;
    }
}
:deep(.ag-cell) {
    border-right: 1px solid #dde2eb;
}
:deep(.ag-header-cell){
    border-right: 1px solid #dde2eb;
}
</style>